// pages/wnGoodsList/wnGoodsList.js
//获取应用实例
var app = getApp()
Page({
  data: {
    searchShow:false,
    modelShow:false,
    showLoading: true,
    noMoretip: false,
    page: 0
  },

  onLoad: function (e) {
    var that = this;
    if (e&&e.id){
      that.setData({
        categoryId: e.id
      });
    }
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    if (e && e.title) {
      that.setData({
        title:e.title
      })
      app.setNavtitle(e.title);
    }else{
      app.setNavtitle('商品列表');
    }
    console.log(that.data.categoryId)
    that.requestGoodList('one');
  },
  // 显示出搜索框
  searchShow:function(){
    var that = this;
    that.setData({
      searchShow:true
    })
  },
  //获得搜索框中的内容
  searchChange:function(e){
    var that =  this;
    that.setData({
      content:e.detail.value
    });
    console.log(that.data.content)
  },
  //搜索相对应的商品
  searchGoods:function(){
    var that = this;
    that.requestGoodList();
  },
  //类型切换
  toggleChange:function(e){
    var that = this;
    var categoryId = e.currentTarget.dataset.categoryid;
    if (e.currentTarget.dataset.model){
      that.setData({
        modelShow: false
      });
    }
    that.setData({
      categoryId: categoryId,
      toViewCategoryId: 'category' + categoryId,
      shopGoods:null,
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestGoodList();
  },
  requestGoodList:function(loadtype){
    var that = this;
    var data = {};
    var page = that.data.page;
    var goodType = that.data.goodType;
    var categoryId = that.data.categoryId ? that.data.categoryId:'';
    var sortType = that.data.sortType;
    if (that.data.content){
      data.keyWord = that.data.content
    }
    data.map = 'applet_mall_goodList';
    data.type = categoryId;
    data.page = page;
    
    console.log(data)
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          if (loadtype=='one'){
            var category = res.data.data.category;
            that.setData({
              category: category,
              toViewCategoryId: 'category' + categoryId
            })
            if (category.length > 0 && !that.data.categoryId){
              that.setData({
                categoryId: category[0].id,
                toViewCategoryId: 'category' + category[0].id
              })
            }
          }
          var allArr = [];
          var initArr = that.data.shopGoods;
          var curArr = res.data.data.goods;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data.goods;
          }
          that.setData({
            shopGoods: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          };
          if (res.data.data.goods.length <= 0 && page == 0) {
            that.setData({
              shopGoods: [],
              noMoretip: false,
              showLoading: false
            });
          };
          console.log(that.data.shopGoods);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              shopGoods: [],
              category:[],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        // wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      searchShow: false,
      content:''
    });
    this.requestGoodList();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestGoodList();
    }
  },
  //打开商品详情
  openGoodDetail: function (e) {
    var that = this;
    var goodid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail?id=' + goodid,
    })
  },
  searchPage: function () {
    wx.navigateTo({
      url: '/pages/searchList/searchList'
    })
  },
  //打开弹窗
  openModel: function () {
    var that = this;
    that.setData({
      modelShow: true
    })
  },
  //关闭弹窗
  closeModel:function(){
    var that = this;
    that.setData({
      modelShow:false
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var title = that.data.title ? that.data.title : '商品列表';
    var id = that.data.categoryId ? that.data.categoryId : '';
    app.getPoint(that);
    return {
      title: title,
      path: '/pages/wnGoodsList/wnGoodsList?enterfrom=share&id=' +id + '&title=' + title
    }
  }
})